<template>
	<view>
		<view class="logo">
		    <image :src="avatar" mode="widthFix" style="width:100px;height: 100rpx;border-radius: 50%;"></image>
		</view>
		
		<!-- <view class="btn" @click="goAuth()">
			微信一键登录
		</view> -->
		<button v-if="checked" class="btn" open-type="getPhoneNumber" @getphonenumber="getPhoneNumber">微信用户一键登录</button>
		<button v-if="!checked" class="btn" @tap="goAuth()">微信用户一键登录</button>
		<view class="rule" @click='chgStatus()' >
			<label>
				<radio :checked="checked" color='#4CA6FF' class="form-radio"/>
				登录代表您已同意 <text class="colorOrange" @click.stop="jumpTo"> 《相伴医路用户协议》</text>
			</label>
		</view>
	</view>
</template>

<script>
	import {
		mapState,
		mapMutations
	} from 'vuex';
	export default {
		data() {
			return {
				checked:false,
				sessionKey:'',
				nickName:'',
				sex:'',
				avatar:'',
				shopName:'',
				accessId:''
			}
		},
		computed: mapState(['loginState', 'userInfo']),
		onLoad(option) {
			let param = this.$store.state.sessionUserInfo
		   	this.sessionKey = param.sessionKey
			this.nickName = param.nickName
			this.sex = param.sex
			this.avatar = param.avatar
		},
		onUnload(e) {
			uni.switchTab({
				url:'../index/index'
			})
		
		},
		methods: {
			...mapMutations(['login', 'logout','setUserInfo']),
			jumpTo(){
				uni.navigateTo({
					url:'Platformstatement'
				})
			},
			// 获取用户信息
			getUserInfo() {
				this.$api.getUserInfo().then((res) => {
					let userInfo = res.data.dataMap
					// this.getUserIn = res.data.dataMap
					console.log(userInfo)
					if (userInfo != null) {
						this.setUserInfo(userInfo)
					}
			
				})
			
			},
			/* 获取用户手机号 */
			getPhoneNumber: function(e) {
				console.log(this.checked)
					let _this = this
					// console.log(e);
					if (e.detail.errMsg == 'getPhoneNumber:fail:access denied') {
						console.log('用户拒绝提供手机号');
					} else {
						console.log('用户同意提供手机号');
						console.log(JSON.stringify(e.rawData))
						console.log(JSON.stringify(e.detail.encryptedData));
						console.log(JSON.stringify(e.detail.iv));
						var encryptedData = e.detail.encryptedData;
						var iv = e.detail.iv;
						this.$api_common.login({
							type:3,
							sessionKey:_this.sessionKey,
							encryptedData:e.detail.encryptedData,
							iv:e.detail.iv,
							sex:_this.sex,
							image:_this.avatar,
							nickName:_this.nickName,
							accessId:'AfcMxdqEempG46jTLPN1hzXRt',
						}).then((res) =>{
							if(res.data.success){
								console.log(res.data.dataMap)
								let userInfo ={
									token:res.data.dataMap.token,
									userInfo:{
										
									}
								}
								_this.login(userInfo)
								uni.switchTab({
									url:'../index/index'
								})
								_this.getUserInfo()

							}else{
								console.log("接口异常")
							}
								
						})
					}

				
			
			},
			chgStatus(){
				if(this.checked){
				    this.checked = false;
				}else{
					this.checked = true;
				}
			},
			goAuth(){
				if(this.checked==false){
				    uni.showToast({
				    	title:'请先同意协议',
						icon:"none"
				    })
				    return;	
				}

				
			}
		}
	}
</script>

<style>
   .logo{
	   padding: 78px 0;
	   text-align: center;
   }
   .form-radio{transform: scale(0.7);}
   .btn{
	   background: #4CA6FF;
	   line-height: 100rpx;
	   border-radius:50px;
	   width: 80%;
	   line-height: 100rpx;
	   font-size: 32rpx;
	   margin:auto;
	   color:white;text-align: center;
   }	
   .rule{font-size: 24rpx;color:#999999;text-align: center;margin:10px 0}
   .colorOrange{
	   font-size: 24rpx;
	   font-weight: 500;
	   color: #4CA6FF;
   }
</style>

